<?php

/**
 * Albireo module
 *
 * @copyright  Copyright (c) 2011 Albireo Solutions
 * @package    Kernel
 */

namespace Celebrio\FileSystem\Drivers;

use Nette\Object;
use Celebrio\FileSystem\Drivers\IDriver;
use Celebrio\FileSystem\DataFile;

/**
 *
 * @author qwei
 */
abstract class AzureStorageDriver extends Object implements IDriver {
    const MAX_BLOB_SIZE = 67108864;

    const MAX_BLOCK_SIZE = 4194304;
    
    protected $connection = null;
    
    protected static $host = "blob.core.windows.net";
    protected static $accountName = null;
    protected static $password  = null;
    
    protected $containerName = null;

    
    protected function getContainer() {
        return (empty($this->containerName) ? 'root' : $this->containerName);
    }
    /*
     * Every file should be stored in some container but containers structure
     * is different for each storage system. 
     */
    protected function getContainerForFile(DataFile $dataFile) {
        
    }
    
    
    public static function setConnectionSettings($accountName, $password) {
        self::$accountName = $accountName;
        self::$password = $password;
    }
    
    protected function getPathForFile(DataFile $dataFile) {
        return $dataFile->getUrl() . $dataFile->getName();
    }    
    
    public function setContainer($containerName) {
        $this->containerName = $containerName;
    }
    
    
    
    protected function getIndexes($numOfBlocks) {
        $numOfIndex = array();
        for ($i = 0; $i < $numOfBlocks; $i++) {
            $numOfIndex[] = $this->generateZeroToBlockID($i);
        }

        return $numOfIndex;
    }
    
    protected function generateZeroToBlockID($part = 0) {
        $returnValue = $part;
        while (strlen($returnValue) < 64) {
            $returnValue = '0' . $returnValue;
        }

        return $returnValue;
    }
    
    
    //TODO:Pochytat výjimky
    public function getBlobsInContainer($containerName, $prefix = '') {
        $storage = $this->getConnectionSettings();
        return $storage->listBlobs($containerName, $prefix);
    }
    
    public function getContainers($prefix = null) {
        $storage = $this->getConnectionSettings();
        return $storage->listContainers($prefix);
    }

}
